<?php
namespace Common\Common;
class Decrypt {
    
    //获得RSA公钥
    function get_rsa_public_key() {
	return '{"rsa_public_key":"B1E6062988679D65FC5DC2D7E903317A6E3CE011BB5CDB4B00A4854BE561A78DC70AE5561657FC29D2D24CEA60CA65CCF83C5AFF017696269535E03AA808FC112445B502F0EC3A8AF559366A284333F8FB05B3EF9634FFF796ADC48BBA61B006629B73DB29193A50C03E92E1504AB2D17EC8207A485CA7E3DF3AD077CD0B99957F265A5588A3F3D57AD7281B9BCC62138E94C3BD7AE3FCD07FB1073DBF873A0E6EAAE10B15CFCB0D879944C325C407AE113A3BF1130FCE93AF87E68E0A1D941EAAF6AB1E9AEE32B844E219170F9966DE55FF4B0EE8C4A70EBCB593284E5F5CA27696ACEE115D01F76779FF4009383C397CE1481F68D20C568A4E9D72BAD4F0A9", "rsa_exponent":"10001"}';
    }
    
    
    //密文解密
    function decrypt($ciphertext) {
$rsa_private_key = openssl_pkey_get_private('-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAseYGKYhnnWX8XcLX6QMxem484BG7XNtLAKSFS+Vhp43HCuVW
Flf8KdLSTOpgymXM+Dxa/wF2liaVNeA6qAj8ESRFtQLw7DqK9Vk2aihDM/j7BbPv
ljT/95atxIu6YbAGYptz2ykZOlDAPpLhUEqy0X7IIHpIXKfj3zrQd80LmZV/JlpV
iKPz1XrXKBubzGITjpTDvXrj/NB/sQc9v4c6Dm6q4QsVz8sNh5lEwyXEB64ROjvx
Ew/Ok6+H5o4KHZQeqvarHpruMrhE4hkXD5lm3lX/Sw7oxKcOvLWTKE5fXKJ2lqzu
EV0B92d5/0AJODw5fOFIH2jSDFaKTp1yutTwqQIDAQABAoIBAQCdJ45UDGs0Uc8C
aUjhUG/aLx5Z93AkFJmKZMxYgrelFAs85eIJ9QoPTG15gyekRRAwTuk4EuvWsxjn
MkG/Bcjfv8JQqNt6ZDEzku8OEqljdVUT8aP6drGU4Ch708//0kehvqA3gHQH3yQl
Yw3mJ0cW6MAUa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w8oa6HFbXg==
-----END RSA PRIVATE KEY-----');


	//1、 解RSA加密
	$rsa_json_aes_json_data = $ciphertext['data'];
//	print_r($rsa_json_aes_json_data['data']);
//	$rsa_json_aes_json_data = $rsa_json_aes_json_data->data;
	$rsa_json_aes_json_data = base64_decode($rsa_json_aes_json_data); // 对密文base64解码

	openssl_private_decrypt($rsa_json_aes_json_data, $json_aes_json_data, $rsa_private_key, OPENSSL_PKCS1_PADDING) // $plaintext为解密明文，解密函数为PHP自带函数
    or die("openssl_private_decrypt failed.");

//    print_r( $json_aes_json_data);


    $json_aes_json_data = json_decode($json_aes_json_data);

//    print_r($json_aes_json_data);
//    echo "\n";

	$aes_json_data = $json_aes_json_data->data;
	$aes_key = $json_aes_json_data->aes_key;
	$aes_iv = $json_aes_json_data->aes_iv;
//	echo "aes_json_data:".$aes_json_data;
	$aes_json_data = urldecode($aes_json_data);

	$aes_json_data = base64_decode($aes_json_data);
//	echo "\n\n\n".$aes_json_data."\n";

	$decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $aes_key, $aes_json_data, MCRYPT_MODE_CBC, $aes_iv);
	$decrypted = substr($decrypted, 0, strrpos($decrypted,"}")+1);
	
	// $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, "18927108027319111918878338275929", "FeRk4IWFIgtluoY4gony5EGRHi3L57/PonKBuGpEIaw=", MCRYPT_MODE_CBC, "1261169378126988");
//	echo "\n\n---------result-----------\n";
//	echo $decrypted;
	return $decrypted;
    }
	
}
?>